﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DynamicProxy
{
    internal class ProxyFactory
    {

        /// <summary>
        /// 创建代理实例
        /// </summary>
        /// <param name="decorated">代理的接口类型</param>
        /// <returns></returns>
        public static T Create<T>()
        {
            var decorated = ServiceHelp.GetService<T>();
            var type = decorated.GetType();
            var interceptAttribut = type.GetCustomAttribute<InterceptAttribut>();
            var interceptor = ServiceHelp.GetService<IInterceptor>(interceptAttribut.Type);
            //创建代理类
            var proxy = new DynamicProxy<T>().Create(decorated, interceptor.AfterAction, interceptor.BeforeAction);
            return proxy;
        }

        /// <summary>
        /// 创建代理实例
        /// </summary>
        /// <param name="decorated">代理的接口类型</param>
        /// <returns></returns>
        //public static T MyCreate<T>()
        //{
        //    var decorated = Activator.CreateInstance(typeof(DynamicProxyTest));
        //    return (T)decorated;
        //}

    }
}
